import pygame

from actor.welcome_button import Button
from actor.welcome_sprite import WelcomeSprite
from game import ScenePassStatus
from scene import BaseScene

"""
欢迎场景类
"""
class WelcomeScene(BaseScene):
    def __init__(self, xiao_tie):
        """
        参数初始化
        :param xiao_tie:
        """
        super(WelcomeScene, self).__init__(xiao_tie,
                                           "./resources/images/welcome/welcome1.jpg")
        self.actor = WelcomeSprite(50, 300)
        pygame.mixer.music.load("resources/sound/地道战.mp3")
        pygame.mixer.music.play(start=20)
        # 开始按钮
        self.play_button = Button('Play', 'red', None, 350, centered_x=True)
        # 推出按钮
        self.exit_button = Button('Exit', 'white', None, 400, centered_x=True)

    def draw_actor(self):
        """
        绘制精灵类
        :return:
        """
        self.actor.draw(self.current_surface, 0, 0)

    def run(self, key_down_flag, key_list):
        """
        实现按钮的显示与鼠标的互动
        :param key_down_flag:
        :param key_list:
        :return:
        """
        if self.actor.pos_x >= 1280:
            if self.play_button.check_click(pygame.mouse.get_pos()):
                self.play_button = Button('Play', 'red', None, 350, centered_x=True)
            else:
                self.play_button = Button('Play', 'white', None, 350, centered_x=True)

            if self.exit_button.check_click(pygame.mouse.get_pos()):
                self.exit_button = Button('Exit', 'red', None, 400, centered_x=True)
            else:
                self.exit_button = Button('Exit', 'white', None, 400, centered_x=True)
            self.play_button.display()
            self.exit_button.display()
            pygame.display.update()
            # 如果鼠标点击
            if pygame.mouse.get_pressed()[0]:
                # 判断是否在开始按钮位置
                if self.play_button.check_click(pygame.mouse.get_pos()):
                    self.pass_status = ScenePassStatus.over
                    pygame.mixer.music.stop()
                # 判断是否在结束按钮位置
                elif self.exit_button.check_click(pygame.mouse.get_pos()):
                    exit(0)
        elif key_list[pygame.K_SPACE]:
            self.actor.pos_x = 1280
        else:
            self.actor.run(key_down_flag, key_list)

    def get_pass_status(self):
        """
        获取游戏状态
        :return:
        """
        if self.pass_status != ScenePassStatus.over:
            return self.pass_status, None
        else:
            return self.pass_status, "TunnelWarScene"
